package question;

import interceptor.InjectConnection;

import java.sql.Connection;
import java.io.File;
import java.io.FileInputStream;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

import utils.DBConnection;
import utils.Var;
import utils.TokenUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.struts2.ServletActionContext;
import setup.Setup;

import javax.servlet.http.HttpSession;

import account.AccountUtils;

/**
 * Created by IntelliJ IDEA.
 * User: Shawn
 * Date: 2009-6-16
 * Time: 21:06:12
 */
public class QuestionAction {

  private Connection con;

  @InjectConnection(autoCommit = DBConnection.AutoCommiteOff)
  public void setCon(Connection con) {
    this.con = con;
  }

  private int cid;

  public String delClassify() throws Exception {
    HttpSession session = ServletActionContext.getRequest().getSession();
    if (!AccountUtils.hasRight(session, Var.RIGHT_QUESTION)) throw new Exception("deny!");

    String allClassifyID = String.valueOf(cid);
    String allYdID = "";

    List<ClassifyInfo> dtList = Classify.getChildren(con, cid);
    for (ClassifyInfo info : dtList) allClassifyID += "," + info.getCid();

    List<ClassifyInfo> ydList = Classify.getYdList(con, cid);
    for (ClassifyInfo info : ydList) {
      allClassifyID += "," + info.getCid();
      allYdID += (allYdID.length() == 0 ? "" : ",") + info.getCid();
    }

    Setup.delSetup(con, cid);
    Classify.delClassify(con, TokenUtils.getIntArray(allClassifyID, ","));
    Question.delQuestion(con, TokenUtils.getIntArray(allYdID, ","));

    return Var.ACTION_SUCCESS;
  }

  public void setCid(int cid) {
    this.cid = cid;
  }

  private File excel;

  public String importQuestion() throws Exception {
    HttpSession session = ServletActionContext.getRequest().getSession();
    if (!AccountUtils.hasRight(session, Var.RIGHT_QUESTION)) throw new Exception("deny!");

    HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(excel));
    HSSFSheet sheet = workbook.getSheetAt(0);

    Map<String, Integer> kmMap = new HashMap<String, Integer>();
    Map<String, Integer> dtMap = new HashMap<String, Integer>();
    Map<String, Integer> ydMap = new HashMap<String, Integer>();

    List<QuestionInfo> list = new ArrayList<QuestionInfo>();
    for (int i = 0; ; i++) {
      HSSFRow row = sheet.getRow(i);
      if (row == null || row.getCell(3) == null) break;

      String km = row.getCell(3).getRichStringCellValue().getString();
      Integer kmID = kmMap.get(km);
      if (kmID == null) kmID = Classify.addClassify(con, new ClassifyInfo(km, QuestionVar.CLASSIFY_TYPE_KM, 0));
      kmMap.put(km, kmID);

      String dt = row.getCell(4).getRichStringCellValue().getString();
      Integer dtID = dtMap.get(dt);
      if (dtID == null) dtID = Classify.addClassify(con, new ClassifyInfo(dt, QuestionVar.CLASSIFY_TYPE_DT, kmID));
      dtMap.put(dt, dtID);

      String yd = row.getCell(6).getRichStringCellValue().getString();
      Integer ydID = ydMap.get(yd);
      if (ydID == null) ydID = Classify.addClassify(con, new ClassifyInfo(yd, QuestionVar.CLASSIFY_TYPE_YD, dtID));
      ydMap.put(yd, ydID);

      String content = row.getCell(7).getRichStringCellValue().getString();
      int tx = QuestionUtils.getTx(row.getCell(8).getRichStringCellValue().getString());
      String answer = QuestionUtils.getAnswer(row.getCell(9).getRichStringCellValue().getString());

      list.add(new QuestionInfo(ydID, tx, content, answer));
    }

    Question.addQuestionList(con, list);
    return Var.ACTION_SUCCESS;
  }

  public void setExcel(File excel) {
    this.excel = excel;
  }
}
